package cn.ctyun.cloudflow.sdk.util;

import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

public class OkHttpUtil {
    private static final String SSL = "SSL";

    public static final X509TrustManager X_509_TRUST_MANAGER = new X509TrustManager() {
        @Override
        public void checkClientTrusted(X509Certificate[] chain, String authType) {
        }

        @Override
        public void checkServerTrusted(X509Certificate[] chain, String authType) {
        }

        @Override
        public X509Certificate[] getAcceptedIssuers() {
            return new X509Certificate[] {};
        }
    };

    public static SSLContext getIgnoreInitedSslContext() {
        try {
            SSLContext sslContext = SSLContext.getInstance(SSL);
            sslContext.init(null, new TrustManager[] {X_509_TRUST_MANAGER}, new SecureRandom());
            return sslContext;
        } catch (NoSuchAlgorithmException| KeyManagementException e) {
            throw new RuntimeException(e);
        }
    }

    public static HostnameVerifier getIgnoreSslHostnameVerifier() {
        return (arg0, arg1) -> true;
    }
}